package com.sanyang.pda.ui.random;

import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sanyang.pda.bean.ByBarcodeBean;
import com.sanyang.pda.bean.ScanBean;
import com.sanyang.pda.bean.SelectBean;
import com.sanyang.pda.bean.request.SamplingSubmitRequest;
import com.sanyang.pda.contract.SamplingContract;
import com.sanyang.pda.dialog.SelectPop;
import com.sanyang.pda.event.SamplingEvent;
import com.sanyang.pda.presenter.SamplingPresenter;
import com.sanyang.pda.utils.UserUtils;
import com.sanyang.pda.view.ScanButView;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.projectbase.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SamplingFragment extends BaseMvpFragment<SamplingPresenter> implements SamplingContract.View {
    @BindView(R.id.tv_tray)
    TextView tvTray;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.tv_spec)
    TextView tvSpec;
    @BindView(R.id.tv_batchNo)
    EditText tvBatchNo;
    @BindView(R.id.et_num)
    EditText etNum;
    @BindView(R.id.comfire)
    Button comfire;
    @BindView(R.id.scan)
    ScanButView scan;
    private String en;
    private String qeNo;
    ByBarcodeBean barcodeBean;
    public SamplingFragment() {
    }

    public SamplingFragment(String en,String qeNo) {
        this.en = en;
        this.qeNo=qeNo;
    }

    @Override
    public SamplingPresenter getPresenter() {
        return new SamplingPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_sampling;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initLinsenterner() {
        scan.setScanCallBack(new ScanButView.ScanCallBack() {
            @Override
            public void callBack(ScanBean scanBean) {
                switch (scanBean.getType()) {
                    case -1://商品
                        //tvGoodsName.setText(scanBean.getCode());
                        presenter.selectArticleInfoByBarcode(scanBean.getCode(),en);
                        break;
                    case 4://托盘
                        tvTray.setText(scanBean.getCode());
                        break;
                }
            }
        });
    }

    @Override
    public void getData() {

    }

    @Override
    public void submitSuccess() {
        EventBus.getDefault().post(new SamplingEvent());
        tvTray.setText("");
        tvGoodsName.setText("");
        tvBatchNo.setText("");
        tvSpec.setText("");
        etNum.setText("");
    }
    SelectBean myselectBean;
    List<SelectBean> selectBeanList=new ArrayList<>();
    @Override
    public void setGoodsList(List<ByBarcodeBean> list) {
        if (list.size() == 0){
            showMessage("未查询到商品信息");
            return;
        }
        if (list.size() == 1){
            tvGoodsName.setText(list.get(0).getBarcode());
            tvSpec.setText(list.get(0).getSpec());
            myselectBean=new SelectBean(list.get(0).getArticleName(),list.get(0).getArticleId());
            myselectBean.setOther(list.get(0).getBarcode());

        }
        if (list.size() > 1){
            selectBeanList.clear();
            for (int i=0;i< list.size();i++){
                ByBarcodeBean byBarcodeBean=list.get(i);
                SelectBean selectBean=new SelectBean(byBarcodeBean.getArticleName(),byBarcodeBean.getArticleId());
                selectBean.setOther(byBarcodeBean.getSpec());
                tvGoodsName.setText(byBarcodeBean.getBarcode());
                selectBeanList.add(selectBean);
            }
            SelectPop selectPop=new SelectPop(mContext, new SelectPop.Onclick() {
                @Override
                public void onClick(SelectBean content) {
                    myselectBean=content;
                    tvSpec.setText(content.getOther());
                }
            });
            selectPop.show(selectBeanList);
        }
    }

    /*@Override
    public void setGoods(ByBarcodeBean byBarcodeBean) {
        this.barcodeBean=byBarcodeBean;
        tvGoodsName.setText(byBarcodeBean.getArticleName());
        tvSpec.setText(byBarcodeBean.getSpec());
    }*/

    @OnClick(R.id.comfire)
    public void onViewClicked() {
        SamplingSubmitRequest samplingSubmitRequest=new SamplingSubmitRequest();
        if (TextUtils.isEmpty(tvTray.getText())) {
            showMessage(getString(R.string.tray) + getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvGoodsName.getText())) {
            showMessage(getString(R.string.commodity) + getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(etNum.getText())) {
            showMessage(getString(R.string.amount) + getString(R.string.not_be_empty));
            return;
        }
        if (TextUtils.isEmpty(tvBatchNo.getText())) {
            showMessage(getString(R.string.batch) + getString(R.string.not_be_empty));
            return;
        }
        samplingSubmitRequest.setEntryNo(en);
        samplingSubmitRequest.setQaNo(qeNo);
        samplingSubmitRequest.setBatchNo(tvBatchNo.getText().toString());
        samplingSubmitRequest.setArticleId(myselectBean.getId());
        samplingSubmitRequest.setPallet(tvTray.getText().toString());
        samplingSubmitRequest.setSpec(myselectBean.getName());
        samplingSubmitRequest.setSamplingNum(etNum.getText().toString());
        samplingSubmitRequest.setCreateBy(UserUtils.getInstance().getUserBean().getUserName());
        presenter.samplingArticleInfoSubmit(samplingSubmitRequest);
    }
}
